package com.hspedu.set_;

import java.util.Date;
import java.util.HashSet;

/**
 * @Author Stringzhua
 * @Date 2024/3/12 16:15
 * description:定义一个Employee类,该类包括:
 * private成员属性name,sal，birthday(MyDate类型)
 * 其中birthday为MyDate类型(属性包括:year,month,day)
 * <p>
 * <p>
 * 1.创建3个Employee放入HashSet中
 * 2.当name和birthday的值相同时,认为是相同员工,不能添加到HashSet集合中
 */
@SuppressWarnings("all")
public class HashSetExercise02 {
    public static void main(String[] args) {
        HashSet<Employee02> employees = new HashSet<>();
        employees.add(new Employee02("张三", 5000, new MyDate(1990, 1, 1)));
        employees.add(new Employee02("李四", 6000, new MyDate(1992, 2, 2)));
        employees.add(new Employee02("王五", 7000, new MyDate(1994, 3, 3)));

        // 尝试添加相同的Employee对象
        Employee02 duplicateEmployee = new Employee02("张三", 5000, new MyDate(1990, 1, 1));
        boolean isAdded = employees.add(duplicateEmployee);
        System.out.println("添加结果： " + isAdded); // 输出：添加结果： false
    }
}

class MyDate {
    int year;
    int month;
    int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
}

@SuppressWarnings("all")
class Employee02 {
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee02(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Employee02 employee = (Employee02) obj;
        return name.equals(employee.name) && birthday.year == employee.birthday.year && birthday.month == employee.birthday.month && birthday.day == employee.birthday.day;
    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + Double.hashCode(sal);
        result = 31 * result + birthday.year;
        result = 31 * result + birthday.month;
        result = 31 * result + birthday.day;
        return result;
    }
}

